package de.koller.worlders.gameplay.inventory;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;
import de.koller.worlders.WSea;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.gameplay.items.Item;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.lwjgl.input.Keyboard;

public class Inventory extends WSea {
	
	private boolean visible = false;
	@Child private final InventoryV invV = new InventoryV();
	private final List<InventoryEntity> content = new ArrayList<>();
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		wroot.getInput().useReference( this );
		
		toggleView();
		manageInventory();
		
		wroot.getInput().unuseReference();
	}

	@Override
	protected void init() {
		super.init();
		
		this.load();
	}

	public void addItem( Class<? extends Item> itemType, int number ) {
		if( number <= 0 ) throw new IllegalArgumentException( "you can't add a negativ amount" );
		
		for( InventoryEntity v : content )
			if( v.getItem().getClass().equals( itemType ) ) {
				v.setNumber( v.getNumber() + number );
				return;
			}
		
		Item i = null;
		try {
			i = itemType.getConstructor( WorldersRoot.class ).newInstance( wroot );
		} catch( Exception ex ) {
			throw new OceanException( "Item must have an constructor with wroot" );
		}
		
		InventoryEntity ie = new InventoryEntity( i );
		ie.setNumber( number );
		content.add( ie );
	}
	
	public boolean removeItem( Class<? extends Item> itemType, int number ) {
		if( number <= 0 ) throw new IllegalArgumentException( "you can't remove a negativ amount" );
		
		for( InventoryEntity v : content )
			if( v.getItem().getClass().equals( itemType ) ) {
				if( v.getNumber() < number )
					return false;
				
				v.setNumber( v.getNumber() - number );
				return true;
			}
		
		return false;
	}
	
	private void manageInventory() {
		if( !visible ) return;
		
		Collections.sort( content );
	}
	
	private void toggleView() {
		if( wroot.getGui().getConsoleInput().isOpen() )
			return;
		
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_ESCAPE ) ) {
			visible = false;
			wroot.getInput().unsetExclusivity();
		}
			
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_I ) ) {
			visible = !visible;
			if( visible )
				wroot.getInput().setExclusivity( this );
			else
				wroot.getInput().unsetExclusivity();
		}
	}
	
	public boolean isVisible() {
		return visible;
	}

	public List<InventoryEntity> getContent() {
		return content;
	}

	public void save() {
		MOut o = new MOut( wroot.getUser().getUserpath() + "inventory.2mi" );
		for( InventoryEntity i : content ) {
			o.writeString( i.getItem().getClass().getName() );
			o.writeInt( i.getNumber() );
		}
		o.close();
	}
	
	public void load() {
		MIn i = new MIn( wroot.getUser().getUserpath() + "inventory.2mi" );
		
		String s = i.readString( 1 )[0];
		while( !s.isEmpty() ) {
			int n = i.readInt( 1 )[0];
			try {
				addItem( (Class<? extends Item>)Class.forName( s ), n );
			} catch ( Exception ex ) {
				System.err.println( s + " not found for inventory! " + ex.toString() );
			}
			s = i.readString( 1 )[0];
		}
		
		i.close();
	}
	
}
